/*
 * Created on May 6, 2004
 *
 * @TODO: Make jar for ant tasks
 */
package org.pyant.tasks;

import java.io.File;
import java.util.ArrayList;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Commandline;

/**
 * @author Ron Smith
 *
  	<py-run script="testscripts/executable.py" python=""
  		pythonpath="" pythonpathref="" optimize="1,2" tabwarning="true">
  		<env key="" value=""/>
  		<arg value=""/>
    </py-run>

    
    <py-compile dir="" optimize="1,2">
    python -OO -c 'import compileall;compileall.compile_dir(".")'
    </py-compile>
    
    <py-test>
    
    </py-test>
    
    @TODO: Probably make this extend ExecTask.
    @TODO: Include an outputfile option, probably via ExecTask, and re-work my article
    example so that that's how sample RSS is fetched
    @TODO: Include working directory option - via ExecTask?
 */
public class PythonRunTask extends PythonBaseTask
{
	protected File scriptFile = null;
	protected ArrayList arguments = null;
//	protected Commandline cmdline = new Commandline();
	protected String command = null;
	
	// Directory to execute from
	protected File dir = null;
	
	public PythonRunTask()
	{
//		cmdline.setExecutable("python");
	}

	public void execute()
	{
		Project project = getProject();
		project.log("PythonRunTask.execute()", Project.MSG_DEBUG);
		project.log("script: " + this.scriptFile, Project.MSG_DEBUG);
		project.log("command: " + this.command, Project.MSG_DEBUG);
		
		
		
		executeScript(project, this.dir, "Failure in running script");
				
//		LogStreamHandler streamHandler = new LogStreamHandler(this, Project.MSG_INFO,
//					Project.MSG_WARN);
//		
//		Execute runner = new Execute(streamHandler, null);
//		
//		setPythonPathInExecute(runner);
//		
//		runner.setAntRun(project);
//		runner.setCommandline(cmdline.getCommandline());
//		try
//		{
//			runner.execute();
//		}
//		catch(IOException ie)
//		{}
		
	}
	
	public void setScript(File script)
	{
		this.scriptFile = script;
//		cmdline.createArgument(true).setValue(this.script);
	}
	
    public Commandline.Argument createArg()
    {
    	Commandline cmdline = getCommandline();
        return cmdline.createArgument();
    }
    
    public void setCommand(String command)
    {
    	this.command = command;
    }
    
    /**
     * Set the working directory the script is executed from.
     * @param dir
     */
    public void setDir(File dir)
    {
    	this.dir = dir;
    }
    
    /*
    public void addArg(Commandline.Argument arg)
    {
    	
    }*/
    
    
	protected void addCommandlineArgs(Commandline cmdline) {
		super.addCommandlineArgs(cmdline);
		
		
		if(this.scriptFile != null)
		{
			cmdline.createArgument(true).setValue(this.scriptFile.getAbsolutePath());
		}

		if(this.command != null)
		{
			cmdline.createArgument(true).setValue("-c");
			cmdline.createArgument(true).setValue(this.command);
		}
		
		super.addCommandlineArgs(cmdline);
	}

}
